package com.hongye.app.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongye.util.JsonUtils;
import com.hongye.util.Utils;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

public class ReturnGoodsModel extends Model<ReturnGoodsModel>{

	private static final long serialVersionUID = -1346574594478390434L;
	
	public static final ReturnGoodsModel dao=new ReturnGoodsModel();
	
	/**
	 * 查询退货详细
	 * @author lifuqiang
	 * 2016-11-14
	 * @param returngoods
	 * @return
	 */
	public static Map<String, Object> deserializeData(ReturnGoodsModel returngoods)  {
		String datajson = returngoods.get("data");
		Map<String, Object> data = JsonUtils.deserialize(datajson);
		return data;
	}
	
	/**
	 * 增加returngoods中的data内容
	 * @author liushaomin
	 * 2016年11月14日
	 * @param returngoods
	 * @param state
	 * @param requireName
	 * @param requiredata
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String getData(ReturnGoodsModel returngoods, int state, String name, String title)  {
		List<Map<String, Object>> list_Traces = new ArrayList<Map<String,Object>>();
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> map_traces = new HashMap<String, Object>();
		if(returngoods.get("data") != null){
			String datajson = returngoods.get("data");
			data = JsonUtils.deserialize(datajson);
			list_Traces = (List<Map<String, Object>>) data.get("Traces");
		}
		map_traces.put("state", state);
		map_traces.put("updateTime", Utils.getDate());
		map_traces.put("name", name);
		map_traces.put("title", title);
		list_Traces.add(map_traces);
		data.put("Traces", list_Traces);
		data.put("updateTime", Utils.getDate());
		return JsonUtils.serialize(data);
	}
	
	/**
	 * 查询退货列表(为买家提供)-分页
	 * @author lifuqiang
	 * 2017-4-12
	 * @param phone
	 * @param pageNumber
	 * @return
	 */
	public Page<ReturnGoodsModel> returnGoodsBybuyerPhone(String phone, int pageNumber){
		String sql="select  r.return_id, r.seller_id, g_order, return_status, return_money, isreturn_goods, r.c_time, update_time ";
		String str = "from return_g r, buyer b where r.phone = '"+phone+"' and r.del = 0 and r.seller_id = b.user_id order by update_time desc";
		Page<ReturnGoodsModel> order_list=dao.paginate(pageNumber, Utils.pageSize, sql,str);
		return order_list;
	}
	
	/**
	 * 查询退货列表(为卖家提供)-分页
	 * @author lifuqiang
	 * 2017-4-12
	 * @param phone
	 * @param pageNumber
	 * @return
	 */
	public Page<ReturnGoodsModel> returnGoodsBySellerPhone(String phone, int pageNumber){
		String sql="select r.return_id, r.del, g_order, return_status, isreturn_goods, r.c_time, update_time ";
		String str = "from return_g r, buyer b where b.phone = '"+phone+"' and r.seller_id = b.user_id order by update_time desc";
		Page<ReturnGoodsModel> order_list=dao.paginate(pageNumber, Utils.pageSize, sql,str);
		return order_list;
	}
	
	/**
	 * 定时器--获取退货申请的订单list
	 * @author lifuqiang
	 * 2016-12-19
	 * @return
	 */
	public List<ReturnGoodsModel> getReturnApplyOrder(){
		String sql = "select re.return_id, re.return_status, re.update_time, re.g_id,re.g_num, re.data, oi.order_sn, oi.order_status, re.c_time as time from order_info oi, return_g re where oi.order_status = 8 and re.isreturn_goods = 0 and re.return_status = 0 order by time desc";//所有申请了退货退款的订单
		List<ReturnGoodsModel> retrun_list = ReturnGoodsModel.dao.find(sql);
		return retrun_list;
	}
	
	/**
	 * 得到商家同意退货申请
	 * @author lifuqiang
	 * 2016-12-19
	 * @return
	 */
	public List<ReturnGoodsModel> getSellerAgreeReturnOrder(){
		String sql = "select re.return_id,re.return_status, re.update_time, re.data, oi.order_sn, re.c_time as time from order_info oi, return_g re where oi.order_status = 10 and re.isreturn_goods = 1 and re.return_status = 0 order by time desc";//所有申请了退货退款的订单
		List<ReturnGoodsModel> retrun_list = ReturnGoodsModel.dao.find(sql);
		return retrun_list;
	}
	
	/** 
	 * 根据订单号 获取所有退货款记录
	 * @author liushaomin
	 * 2017年2月23日
	 * @param ordersn
	 * @return
	 */
	public static List<ReturnGoodsModel> getByG_order(String ordersn){
		String sql = "select * from return_g where g_order = '"+ordersn+"'";
		return ReturnGoodsModel.dao.find(sql);
	}
	
	/**
	 * 根据订单号 查询退货款记录
	 * @author liushaomin
	 * 2017年2月23日
	 * @param ordersn
	 * @param isreturn_goods 0为只退款，1为退货
	 * @return
	 */
	public static ReturnGoodsModel getByG_order(String ordersn, int isreturn_goods){
		String sql = "select * from return_g where g_order = '"+ordersn+"'and isreturn_goods = "+ isreturn_goods +"";
		return ReturnGoodsModel.dao.findFirst(sql);
	}
	
	/**
	 * 根据订单号 查询退货款详情
	 * @author lifuqiang
	 * 2017-4-13
	 * @param ordersn
	 * @return
	 */
	public static ReturnGoodsModel getReturnGoodsByorder(String ordersn){
		String sql = "select return_id, seller_id, buy_id, return_reason, return_status, return_money, isreturn_goods, c_time from return_g where g_order = '"+ordersn+"'";
		return ReturnGoodsModel.dao.findFirst(sql);
	}
	
	/**
	 * 通过订单号判断是否存在退款记录
	 * @author lifuqiang
	 * 2017-2-23
	 * @param ordersn
	 * @return
	 */
	public static boolean isReturnByOrdersn(String ordersn){
		String sql = "select * from return_g where g_order = '"+ordersn+"' and isreturn_goods = 0";
		List<ReturnGoodsModel> returnlist = dao.find(sql);
		boolean boo = false;
		if(returnlist.size()>0){
			boo = true;
		}
		return boo;
	}
	
	/**
	 * 通过订单号获取退货记录
	 * @author lifuqiang
	 * 2017-2-23
	 * @param ordersn
	 * @return
	 */
	public ReturnGoodsModel getReturnGoods(String ordersn){
		String sql = "select * from return_g where g_order = '"+ordersn+"'";
		return dao.findFirst(sql);
	}

	/**
	 * 获取退货款订单数量-买家
	 * @author liushaomin
	 * 2017年5月4日
	 * @param phone
	 * @param instatus
	 * @return
	 */
	public static Long queryIntByBuyer(String phone) {
		return Db.queryLong("select count(1) from return_g where phone =" + phone + " and del = 0");
	}

	/**
	 * 获取退货款订单数量-商家
	 * @author liushaomin
	 * 2017年5月4日
	 * @param phone
	 * @param sellerid
	 * @return
	 */
	public static Long queryIntBySeller(String seller_phone) {
		return Db.queryLong("select count(1) from return_g r, buyer b where b.phone = "+ seller_phone +" and r.seller_id = b.user_id");
	}
}
